package com.emato.cus.controller;

import com.emato.cus.dto.CusHeartRateInfoDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hhq
 * @version 1.0.0
 * 2020/6/30
 */
@RestController
public class CusHeartRateMonitorController {
    private static final Logger LOGGER = LoggerFactory.getLogger(CusHeartRateMonitorController.class);

    //心跳请求的服务器map,key为port
    public static Map<String, CusHeartRateInfoDto> map = new HashMap<>();

    @RequestMapping("/heartRate")
    public String show(String port){
        //最后一次请求时间
        long requestTime = new Date().getTime();

        if (port == null){
            LOGGER.info("心跳正常,port:{},时间:{}",port,requestTime);
            return null;
        }

        if(map.get(port)==null){
            CusHeartRateInfoDto c = new CusHeartRateInfoDto();
            c.setPort(port);
            map.put(port,c );
        }
        long l = requestTime - (map.get(port).getRequestTime()==0?(requestTime-3000):map.get(port).getRequestTime());
        map.get(port).setRequestIntervalTime(l);
        map.get(port).setRequestTime(requestTime);
        map.get(port).setRequest(false);

        LOGGER.info("心跳正常,port:{},时间:{}",port,requestTime);
        return "ok";
    }
}
